﻿using System;
using System.Collections.Generic;
using System.Text;
using Best.SmartCms.Service.BizException;

namespace Best.SmartCms.Service.Common
{
    /// <summary>
    /// 验证
    /// </summary>
    public static class Check
    {
        /// <summary>
        /// 判断对象是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static T NotNull<T>(T value, string parameterName)
        {
            if (value == null)
            {
                throw new ParameterNullException($"参数：{parameterName}不能为空！");
            }

            return value;
        }

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static string NotNullOrEmpty(string value, string parameterName)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ParameterNullException($"参数：{parameterName}不能为空！");
            }

            return value;
        }

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static string NotNullOrWhiteSpace(string value, string parameterName)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ParameterNullException($"参数：{parameterName}不能为空！");
            }

            return value;
        }

        /// <summary>
        /// 判断集合是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ICollection<T> NotNullOrEmpty<T>(ICollection<T> value, string parameterName, string message = "")
        {
            if (value == null || value.Count < 1)
            {
                var msg = $"参数：{parameterName}不能为空！";
                if (message != "")
                {
                    msg = message;
                }

                throw new ParameterNullException(msg);
            }

            return value;
        }
    }
}
